package 链表;

/**
 * 给定一个单链表，把所有的奇数节点和偶数节点分别排在一起。请注意，这里的奇数节点和偶数节点指的是节点编号的奇偶性，而不是节点的值的奇偶性
 * 输入: 1->2->3->4->5->NULL
 * 输出: 1->3->5->2->4->NULL
 * User: zhaixiaolong Date:2024/1/11 16:42 ProjectName: algorithm Version: V
 */
public class 奇偶数链表 {

    /**
     * 初始化链表
     *
     * @return
     */
    public static Node initData() {
        Node node1 = new Node();
        node1.value = 1;
        Node node2 = new Node();
        node2.value = 2;
        Node node3 = new Node();
        node3.value = 3;
        Node node4 = new Node();
        node4.value = 4;
        Node node5 = new Node();
        node5.value = 5;

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        return node1;
    }

    public static void main(String[] args) {
        Node head = initData();
        Node newHead = generateNewNode(head);
    }

    public static Node generateNewNode(Node head) {
        Node head1 = new Node(), head2 = new Node();
        Node temp1 = head1,temp2 = head2;
        int i = 1;
        while (head != null) {
            if (i % 2 == 0) {
                head2.next = head;
                head2 = head2.next;
            } else {
                head1.next = head;
                head1 = head1.next;
            }
            i++;
            head = head.next;
        }
        head1.next = temp2.next;
        head2.next = null;
        return temp1.next;
    }

}
